package com.example.stuwork.controller;

import com.example.stuwork.common.Result;
import com.example.stuwork.pojo.Department;
import com.example.stuwork.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学院管理控制器
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 获取所有学院
     * 
     * @return 学院列表
     */
    @GetMapping
    public Result<List<Department>> getAllDepartments() {
        List<Department> departments = departmentService.findAll();
        return Result.success("获取学院列表成功", departments);
    }

    /**
     * 根据学院编号获取学院
     * 
     * @param deptNumber 学院编号
     * @return 学院信息
     */
    @GetMapping("/{deptNumber}")
    public Result<Department> getDepartment(@PathVariable String deptNumber) {
        Department department = departmentService.getByNumber(deptNumber);
        if (department != null) {
            return Result.success("获取学院信息成功", department);
        } else {
            return Result.error("学院不存在");
        }
    }

    /**
     * 添加学院
     * 
     * @param department 学院信息
     * @return 添加结果
     */
    @PostMapping
    public Result<Department> addDepartment(@RequestBody Department department) {
        // 参数校验
        if (department == null) {
            return Result.error("学院信息不能为空");
        }
        if (department.getDeptNumber() == null || department.getDeptNumber().trim().isEmpty()) {
            return Result.error("学院编号不能为空");
        }
        if (department.getDeptCode() == null || department.getDeptCode().trim().isEmpty()) {
            return Result.error("学院代码不能为空");
        }
        if (department.getDeptName() == null || department.getDeptName().trim().isEmpty()) {
            return Result.error("学院名称不能为空");
        }

        // 检查是否已存在
        if (departmentService.existsByNumber(department.getDeptNumber())) {
            return Result.error("学院编号已存在");
        }
        if (departmentService.existsByCode(department.getDeptCode(), null)) {
            return Result.error("学院代码已存在");
        }
        if (departmentService.existsByName(department.getDeptName(), null)) {
            return Result.error("学院名称已存在");
        }

        boolean success = departmentService.addDepartment(department);
        if (success) {
            Department savedDept = departmentService.getByNumber(department.getDeptNumber());
            return Result.success("添加学院成功", savedDept);
        } else {
            return Result.error("添加学院失败");
        }
    }

    /**
     * 更新学院信息
     * 
     * @param deptNumber 学院编号
     * @param department 学院信息
     * @return 更新结果
     */
    @PutMapping("/{deptNumber}")
    public Result<Department> updateDepartment(@PathVariable String deptNumber, @RequestBody Department department) {
        // 参数校验
        if (department == null) {
            return Result.error("学院信息不能为空");
        }

        // 确保路径中的编号与请求体一致
        department.setDeptNumber(deptNumber);

        // 检查学院是否存在
        if (!departmentService.existsByNumber(deptNumber)) {
            return Result.error("学院不存在");
        }

        // 检查代码和名称唯一性
        if (department.getDeptCode() != null && !department.getDeptCode().trim().isEmpty() 
                && departmentService.existsByCode(department.getDeptCode(), deptNumber)) {
            return Result.error("学院代码已被其他学院使用");
        }
        if (department.getDeptName() != null && !department.getDeptName().trim().isEmpty() 
                && departmentService.existsByName(department.getDeptName(), deptNumber)) {
            return Result.error("学院名称已被其他学院使用");
        }

        boolean success = departmentService.updateDepartment(department);
        if (success) {
            Department updatedDept = departmentService.getByNumber(deptNumber);
            return Result.success("更新学院成功", updatedDept);
        } else {
            return Result.error("更新学院失败");
        }
    }

    /**
     * 删除学院
     * 
     * @param deptNumber 学院编号
     * @return 删除结果
     */
    @DeleteMapping("/{deptNumber}")
    public Result<Boolean> deleteDepartment(@PathVariable String deptNumber) {
        // 检查学院是否存在
        if (!departmentService.existsByNumber(deptNumber)) {
            return Result.error("学院不存在");
        }

        boolean success = departmentService.deleteDepartment(deptNumber);
        if (success) {
            return Result.success("删除学院成功", true);
        } else {
            return Result.error("删除学院失败，可能存在关联数据");
        }
    }

    /**
     * 搜索学院
     * 
     * @param keyword 关键词
     * @return 学院列表
     */
    @GetMapping("/search")
    public Result<List<Department>> searchDepartments(@RequestParam(required = false) String keyword) {
        List<Department> departments = departmentService.search(keyword);
        return Result.success("搜索学院成功", departments);
    }
} 